package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicine.entity.MedicineInventorydeliveryEntity;
import city.spring.modules.medicine.repository.MedicineInventorydeliveryRepository;
import city.spring.modules.medicine.service.*;
import city.spring.modules.medicine.vo.MedicineInventorydeliveryVO;
import city.spring.modules.system.service.UserService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * Service：药品盘点出库表
 *
 * @author 谢金婷
 * @date 2020-07-13 11:24:32
 */
@CacheConfig(cacheNames = {MedicineInventorydeliveryServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicineInventorydeliveryServiceImpl extends ServiceImpl<MedicineInventorydeliveryRepository, MedicineInventorydeliveryEntity> implements MedicineInventorydeliveryService {

    private final MedicineInventoryService medicineinventoryService;
    private final MedicineService medicineService;
    private final MedicineInventorydeliveryRepository medicineInventorydeliveryRepository;
    private final UserService userService;
    private final MedicinestorehouseService medicinestorehouseService;
    private final MedicineinventoryMedicineExtService medicineinventoryMedicineExtService;

    public MedicineInventorydeliveryServiceImpl(MedicineInventoryService medicineinventoryService, MedicineService medicineService, MedicineInventorydeliveryRepository medicineInventorydeliveryRepository, UserService userService, MedicinestorehouseService medicinestorehouseService, MedicineinventoryMedicineExtService medicineinventoryMedicineExtService) {
        this.medicineinventoryService = medicineinventoryService;
        this.medicineService = medicineService;
        this.medicineInventorydeliveryRepository = medicineInventorydeliveryRepository;
        this.userService = userService;
        this.medicinestorehouseService = medicinestorehouseService;
        this.medicineinventoryMedicineExtService = medicineinventoryMedicineExtService;
    }

    @Override
    public void saveMedicineInventorydelivery(MedicineInventorydeliveryEntity entity, @NonNull Principal user) {

//        // 获取盘点记录信息
//        MedicineinventoryMedicineExtEntity medicineinventoryEntity = medicineinventoryMedicineExtService.getById(entity.getInventoryId());
//        //查找仓库管理员
//        MedicinestorehouseEntity medicinestorehouseEntity = medicinestorehouseService.getById(medicineinventoryEntity.getStorehouseId());
//        String userId = medicinestorehouseEntity.getUserId();
//        //当前用户
//        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
//        String userEntityId = userEntity.getId();
//
//        //判断当前用户=仓库管理员
//        if (userId.equals(userEntityId)) {
//            /**
//             * 盘点出库信息
//             */
//            // 盘点计划
//            entity.setMedicineinventoryId(medicineinventoryEntity.getMedicineinventoryId());
//            // 药品id
//            entity.setMedicineId(Integer.valueOf(medicineinventoryEntity.getMedicineId()));
//            //损益值
//            entity.setProfitAndLoss(medicineinventoryEntity.getProfitAndLoss());
//            // 单位
//            entity.setUnit(medicineinventoryEntity.getUnit());
//            // 出库日期
//            entity.setDeliverydate(new Date());
//            //设置登记人员为当前登录用户
//            entity.setUsersId(userEntity.getId());
//
//            /**
//             * 更新药品信息
//             */
//            // 根据盘点记录药品id获取药品信息
//            MedicineEntity medicineEntity = medicineService.getById(medicineinventoryEntity.getMedicineId());
//            //药品盘点出库数量=原来的盘点出库数量+现在的盘点入库数量
//            //1.获取原来的盘点出库数量
//            BigDecimal deliveryInventoryAmount = medicineEntity.getDeliveryInventoryAmount() == null ? BigDecimal.ZERO : medicineEntity.getDeliveryInventoryAmount();
//            medicineEntity.setDeliveryInventoryAmount(deliveryInventoryAmount.add(entity.getDeliveryquantity()));
//            // 更新库存
//            //1.获取原来的库存数量
//            BigDecimal quantity = medicineEntity.getQuantity() == null ? BigDecimal.ZERO : medicineEntity.getQuantity();
//            medicineEntity.setQuantity(quantity.subtract(entity.getDeliveryquantity()));
//            medicineService.updateMedicine(medicineEntity);
//        } else {
//            throw new RuntimeException("您不能进行盘点出库。");
//        }
//

        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicineInventorydelivery(MedicineInventorydeliveryEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicineInventorydelivery(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMedicineInventorydelivery(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MedicineInventorydeliveryEntity> getDeliveryByInventoryId(Pageable pageable, String inventoryId) {
        IPage<MedicineInventorydeliveryEntity> page = MyBatisUtils.toPage(pageable);
        return medicineInventorydeliveryRepository.getDeliveryByInventoryId(page, lambdaQuery().getWrapper(), inventoryId);
    }

    @Override
    public MedicineInventorydeliveryEntity getById(String id) {
        return medicineInventorydeliveryRepository.getById(id);
    }

    @Override
    public IPage<MedicineInventorydeliveryVO> listPage(Page<? extends MedicineInventorydeliveryEntity> page, Wrapper<? extends MedicineInventorydeliveryEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}